package com.hula.onion.book.server.spider.platforms.yirendai;

import com.hula.onion.book.server.common.CommonResponse;
import com.hula.onion.book.server.common.LoginForm;
import com.hula.onion.book.server.config.ResponseStatusEnum;
import com.hula.onion.book.server.utils.RegexParserUtils;
import org.springframework.stereotype.Component;
import org.springframework.util.StringUtils;

import java.util.HashMap;
import java.util.Map;

/**
 * Description:
 * Create Date:2018/1/17 19:49
 *
 * @author liangbing
 * @version 1.0
 */
@Component
public class YiRenDaiSpiderHelper {

    /**
     * 组装请求参数
     *
     * @param form
     * @return
     */
    public Map<String, Object> installRequestParams(LoginForm form) {

        Map<String, Object> map = new HashMap<>(16);

        map.put("username", form.getUserName());
        map.put("password", form.getPassword());
        map.put("fromSite", "YRD");
        map.put("redirectURI", YiRenDaiSpider.GET_ACCOUNT_INFO_URL);
        map.put("rememberMe", 0);
        //验证码相关
        map.put("scene", "login");
        map.put("csessionid", "");
        map.put("token", "");
        map.put("sig", "");

        return map;

    }

    /**
     * 解析账户余额
     *
     * @param html
     * @return
     */
    public CommonResponse parseHtml(String html) {

        CommonResponse commonResponse = new CommonResponse(ResponseStatusEnum.FAIL);

        //处理错误信息
        String beginRegex = "<div class=\"m-row-error s_nick_user\">\n" +
                "\t\t\t\t \t ";
        String endRegex = "\n" +
                "\t\t\t\t \t\n" +
                "\t\t\t\t\t</div>";
        String result = new RegexParserUtils(beginRegex, endRegex, html, RegexParserUtils.TEXTTEGEX).getText();
        if (!StringUtils.isEmpty(result)) {
            commonResponse.setMsg(result);
            commonResponse.setCode(ResponseStatusEnum.ACCOUNT_ERROR.getCode());

            return commonResponse;
        }

        beginRegex = "<div class=\"m-row-error s_user_pwd1\">";
        endRegex = "</div>";
        result = new RegexParserUtils(beginRegex, endRegex, html, RegexParserUtils.TEXTTEGEX).getText();
        if (!StringUtils.isEmpty(result)) {
            commonResponse.setMsg(result);
            commonResponse.setCode(ResponseStatusEnum.ACCOUNT_ERROR.getCode());

            return commonResponse;
        }

        beginRegex = "<div class=\"m-row-error s_srand\">\n" +
                "\t\t\t\t\t";
        endRegex = "\n" +
                "\t\t\t\t\t\n" +
                "\t\t\t\t\t";
        result = new RegexParserUtils(beginRegex, endRegex, html, RegexParserUtils.TEXTTEGEX).getText();
        if (!StringUtils.isEmpty(result)) {
            commonResponse.setMsg(result);
            commonResponse.setCode(ResponseStatusEnum.ACCOUNT_ERROR.getCode());

            return commonResponse;
        }

        beginRegex = "<div class=\"m-row-error s_srand\">";
        endRegex = "</div>";
        result = new RegexParserUtils(beginRegex, endRegex, html, RegexParserUtils.TEXTTEGEX).getText();
        if (!StringUtils.isEmpty(result)) {
            commonResponse.setMsg(result);
            commonResponse.setCode(ResponseStatusEnum.ACCOUNT_ERROR.getCode());

            return commonResponse;
        }

        //处理账户数据
        beginRegex = "<h2 class=\"tips\">当前出借账户现金总额：<span class=\"money\">";
        endRegex = "</span> 元";
        result = new RegexParserUtils(beginRegex, endRegex, html, RegexParserUtils.TEXTTEGEX).getText();
        if(!StringUtils.isEmpty(result)){
            commonResponse = new CommonResponse(ResponseStatusEnum.SUCCESS);
            commonResponse.setData(result);
        }

        return commonResponse;
    }


}
